<?php

namespace App\Http\Controllers\Admin;

use App\Models\Project\Progress;
use App\Models\Project\ProjectType;
use App\Services\Project\ProgressService;
use App\Traits\Admin\SuccessOrFailure;
use Illuminate\Http\Request;

class ProjectTypeController extends Controller
{

    use SuccessOrFailure;
    protected $progressService;
    public function __construct(ProgressService $progressService )
    {
        $this->progressService = $progressService;
    }

    private $permission = 'common.project_type';

    public function index()
    {
        $this->canOrFailure($this->permission);

        $data = ProjectType::query()->orderBy('id')->get();
        return view('admin.project_type.index', compact('data'));
    }

    public function create()
    {
        $this->canOrFailure($this->permission);
        $implement_progresses = $this->progressService->getProgressByStage(Progress::STAGE_IMPLEMENT);
        return view('admin.project_type.create', compact('implement_progresses'));
    }

    public function store(Request $request)
    {
        $this->canOrFailure($this->permission);
        list($attributes, $progresses) = $this->buildData($request);
        $type = ProjectType::create($attributes);
        $type->progress()->sync($progresses);
        return $this->success('', route('admin.projectType.index'));
    }

    public function edit($id)
    {
        $this->canOrFailure($this->permission);
        $type = $this->getProjectTypeOrFailure($id);
        $contain_progresses = $type->progress ? $type->progress->pluck('id')->toArray():[];
        $implement_progresses = $this->progressService->getProgressByStage(Progress::STAGE_IMPLEMENT);
        return view('admin.project_type.edit', compact('type', 'implement_progresses', 'contain_progresses'));
    }

    public function update(Request $request, $id)
    {
        $this->canOrFailure($this->permission);
        $type = $this->getProjectTypeOrFailure($id);
        list($attributes, $progresses) = $this->buildData($request);
        $type->update($attributes);
        $type->progress()->sync($progresses);

        return $this->success('', route('admin.projectType.index'));
    }

    public function buildData($request)
    {
        $this->validate($request, [
            'name' => 'required',
            'number' => 'required'],
            ['name.required' => '请填写名称！',
                'number.required' => '请填写编号！',
            ]
        );
        $progresses = $request->input('progresses') ?? [];
        $attributes = [
            'name' => $request->input('name'),
            'number' => $request->input('number'),
            'default_professional_uid' => $request->input('default_professional_uid') ?? 0,
        ];
        return [$attributes, $progresses];
    }

    public function destroy($id)
    {
        $this->canOrFailure($this->permission);
        $type = $this->getProjectTypeOrFailure($id);
        $type->delete();
        return $this->success();
    }

    private function getProjectTypeOrFailure($id)
    {
        $type = ProjectType::query()->find($id);
        if (!$type) {
            response($this->failure('类型不存在！'), 404)->send();
            exit;
        }
        return $type;
    }
}
